package com.hinnotek.amoeba.ui.activity.old

import android.os.Bundle
import androidx.activity.viewModels
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.lee.common.base.BaseActivity
import com.hinnotek.amoeba.R
import com.hinnotek.amoeba.adapters.old.ImageAdapter
import com.hinnotek.amoeba.bean.old.ProductManagermentDetailBean
import com.hinnotek.amoeba.bean.old.ProductTypePictures
import com.hinnotek.amoeba.databinding.ActivityProductManagermentAddBinding
import com.hinnotek.amoeba.dialog.CustomDialog
import com.hinnotek.amoeba.utilities.IntentUtils
import com.hinnotek.amoeba.utilities.ToastUtils
import com.hinnotek.amoeba.viewmodels.ProductViewModel
import com.lee.common.network.net.ObserverState
import dagger.hilt.android.AndroidEntryPoint
import java.io.Serializable
import java.util.*

@AndroidEntryPoint
class ProductManagermentAddActivity : BaseActivity<ActivityProductManagermentAddBinding>() {

    private var customDialog: CustomDialog? = null
    private var productTypeId = 0
    private var myTitle: String? = null
    private var productTypeName: String? = null
    private var productPrice: String? = null
    private var mAdapter: ImageAdapter? = null
    private val productTypePictures = mutableListOf<ProductTypePictures>()

    private val viewModel by viewModels<ProductViewModel>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setLeftVisible(true)
        setTitle("产品结算价格")

        setContentView(R.layout.activity_product_managerment_add)
    }

    override fun initVew() {
        bindingView.superButtonSubmit.setOnClickListener {
            val bundle = Bundle()
            bundle.putInt("productTypeId", productTypeId)
            bundle.putString("title", myTitle)
            bundle.putString("productTypeName", productTypeName)
            bundle.putString("productPrice", productPrice)
            bundle.putSerializable("productTypePictures", productTypePictures as Serializable)
            IntentUtils.startActivity(
                this@ProductManagermentAddActivity,
                ProductManagermentModifyActivity::class.java,
                bundle
            )
            finishActivity()
        }
    }

    override fun setData() {
        myTitle = intent.getStringExtra("title")
        productTypeId = intent.getIntExtra("productTypeId", 0)
        val manager = GridLayoutManager(mContext, 3, RecyclerView.VERTICAL, false)
        bindingView.rlvSettlementPrice.layoutManager = manager
        bindingView.rlvSettlementPrice.adapter = ImageAdapter().also { mAdapter = it }
        customDialog = CustomDialog(mContext, "正在加载...")
        customDialog?.show()
        getProductTypeById(productTypeId)
    }

    /**
     * app端APP端查询单个带图片地址的产品接口
     * @param productTypeId
     */
    private fun getProductTypeById(productTypeId: Int) {
        viewModel.getProductTypeById(productTypeId)
            .observe(this, object : ObserverState<ProductManagermentDetailBean>() {
                override fun onDataChange(data: ProductManagermentDetailBean) {
                    customDialog?.dismiss()
                    productTypeName = data.productTypeName
                    productPrice = data.productPrice.toString()
                    bindingView.tvProductName.text = myTitle
                    bindingView.tvProductModel.text = productTypeName
                    bindingView.tvSettlementPrice.text = productPrice
                    val list1: MutableList<String> = ArrayList()
                    for (index in data.productTypePictures) {
                        productTypePictures.add(index)
                        list1.add(index.pictureUrl)
                        mAdapter?.setList(list1)
                    }
                    super.onDataChange(data)
                }

                override fun onError(msg: String?, code: Int) {
                    ToastUtils.showShort(mContext, msg)
                    customDialog?.dismiss()
                    super.onError(msg, code)
                }
            })
    }

}